/*
 * CollaborativeWorksDao.java
 *
 * Created on November 9, 2008, 7:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.CollaborativeWorks;

/**
 *
 * @author JollkY
 */
public class CollaborativeWorksDao implements Crud {

    private int state;
    private CollaborativeWorks cw;
    private List collaborativeWorks;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();

    /** Creates a new instance of CollaborativeWorksDao */
    public CollaborativeWorksDao() {
    }

    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }

        return this.state;
    }

    public List<Object> getList() {
        this.collaborativeWorks = new ArrayList();
        try {
            em.getTransaction().begin();
            this.collaborativeWorks = em.createQuery("SELECT cw FROM CollaborativeWorks cw").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return this.collaborativeWorks;
    }
    
    public List getListBySubject(long idSubject) {
        collaborativeWorks = new ArrayList();
        try {
            em.getTransaction().begin();
            collaborativeWorks = em.createQuery("SELECT cw FROM CollaborativeWorks cw " +
                    "WHERE cw.subject.id="+idSubject).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return collaborativeWorks;
    }
    
    public List getListByMember(long idMember) {
        collaborativeWorks = new ArrayList();
        try {
            em.getTransaction().begin();
            collaborativeWorks = em.createQuery("SELECT cw FROM CollaborativeWorks cw " +
                    "WHERE cw.creator.id="+idMember).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return collaborativeWorks;
    }

    public Object read(long id) {
        this.cw = new CollaborativeWorks();
        try {
            em.getTransaction().begin();
            this.cw = em.find(CollaborativeWorks.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return this.cw;
    }

    public int update(Object obj) {
        this.cw = new CollaborativeWorks();
        try {
            em.getTransaction().begin();
            CollaborativeWorks objCw = (CollaborativeWorks) obj;
            cw = em.find(CollaborativeWorks.class, objCw.getId());
            cw.setCreator(objCw.getCreator());
            cw.setDownloaded(objCw.getDownloaded());
            cw.setFullContent(objCw.getFullContent());
            cw.setHasDocument(objCw.getHasDocument());
            cw.setListWorker(objCw.getListWorker());
            cw.setPublishes(objCw.getPublishes());
            cw.setShortDescription(objCw.getShortDescription());
            cw.setStatus(objCw.getStatus());
            cw.setSubject(objCw.getSubject());
            cw.setTitle(objCw.getTitle());
            cw.setUpdates(objCw.getUpdates());
            cw.setWrites(objCw.getWrites());

            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    public int delete(long id) {
        try {
            cw = new CollaborativeWorks();
            em.getTransaction().begin();
            cw = em.find(CollaborativeWorks.class, id);
            em.remove(cw);

            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

}
